﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VINETLIFE.BL;
using VINETLIFE.DO;
using System.Data;
using System.Web.UI.HtmlControls;

namespace VINETLIFE
{
    public partial class Promotions : System.Web.UI.Page
    {
        cmsProductBL data = new cmsProductBL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SEO();
            }
            LoadProducts(data.SelectFrontPage(0,GetLocationId(),0,-1,-1,1,-1,-1),rptNewProduct,CollectionPager1,12);
        }
        protected int GetLocationId()
        {
            int locationId = 0;
            if (Session["LocationId"] == null)
            {
                locationId = 0;
            }
            else
            {
                locationId = int.Parse(Session["LocationId"].ToString());
            }
            return locationId;
        }
        protected void SEO()
        {
            Page.Title = Ultility.WebTitle();

            sysConfigDO objConfig = new sysConfigDO();
            objConfig.ConfigID = 2;

            objConfig = new sysConfigBL().Select(objConfig);


            HtmlMeta metaKey = new HtmlMeta();
            metaKey.Name = "keywords";
            metaKey.Content = objConfig.ConfigValue;
            Page.Header.Controls.Add(metaKey);


            objConfig.ConfigID = 3;
            objConfig = new sysConfigBL().Select(objConfig);
            HtmlMeta metaDesc = new HtmlMeta();
            metaDesc.Name = "description";
            metaDesc.Content = objConfig.ConfigValue;
            Page.Header.Controls.Add(metaDesc);

            Page.Title = "Sản phẩm khuyến mãi | " + Ultility.WebTitle();
        }
        private void LoadProducts(DataTable dt, Repeater rpt, SiteUtils.CollectionPager collect, int pagesize)
        {
            if (dt.Rows.Count > 0)
            {
                collect.MaxPages = 10000;

                collect.PageSize = pagesize; // số items hiển thị trên một trang

                collect.DataSource = new DataView(dt, "", "", DataViewRowState.CurrentRows);

                collect.BindToControl = rpt;
                rpt.DataSource = collect.DataSourcePaged;
                rpt.DataBind();
            }
            else { }
        }
        protected void rptNewProduct_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                DataRowView drv = (DataRowView)e.Item.DataItem;

                bool isNew = (bool)drv["IsNew"];
                bool isPromotion = (bool)drv["IsPromotion"];
                Int64 price = (Int64)drv["Price"];
                Int64 salePrice = (Int64)drv["SalePrice"];
                double percent = 0;
                try
                {
                    percent = (double)(((price - salePrice) * 100) / price);
                }
                catch { percent = 0; }

                Label lblType = (Label)e.Item.FindControl("lblType");
                Panel pnlPromotion = (Panel)e.Item.FindControl("pnlPromotion");
                Panel pnlNonPromotion = (Panel)e.Item.FindControl("pnlNonPromotion");
                Literal ltrSale = (Literal)e.Item.FindControl("ltrSale");
                pnlPromotion.Visible = false;
                pnlNonPromotion.Visible = true;
                if (isNew)
                {
                    lblType.Text = "New";
                    pnlPromotion.Visible = false;
                    pnlNonPromotion.Visible = true;
                }
                if (isPromotion)
                {
                    lblType.Text = "Sale";
                    pnlPromotion.Visible = true;
                    pnlNonPromotion.Visible = false;
                    ltrSale.Text = percent + "%";
                }
                if (isNew && isPromotion)
                {
                    lblType.Text = "Sale";
                    pnlPromotion.Visible = true;
                    pnlNonPromotion.Visible = false;
                    ltrSale.Text = percent + "%";
                }
            }
        }

        protected string FriendlyUrl(string s)
        {
            return Ultility.Change_AV(s);
        }

    }
}